package action.bookinfo;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import bizlogic.bookinfo.BookInfoService;
import bizlogic.bookinfo.domain.BookInfo;

@WebServlet("/bookinfoaddaction")
public class BookInfoAddAction extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    BookInfoService bookInfoService = new BookInfoService();


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        addBookInfo(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    /*
     * 功能需求：实例化一个Bookinfo实体，调用service层的相关方法，新增图书信息
     */
    public void addBookInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data = request.getParameter("data");
        BookInfo bookInfo = JSON.parseObject(data, BookInfo.class);
        bookInfo.setBookid(UUID.randomUUID().toString());
        String str = bookInfoService.addBookInfo(bookInfo);
        response.getWriter().write(str);
    }

}
